<?php
namespace Admin\Controller;

class SystemController extends AdminController
{
    public function seting(){
        $this->display();
    }

    public function clearCache(){
        $key = I('key');
        $where['name'] = array('like',"%$key%");
        $communityData = M('Community')->where($where)->find();
        $communityId = $communityData['id'];
        if ($communityId && $key) {
            $this->reName($communityId);
//            R('Service/Room/json',array('','json'));
//            R('Service/Room/json',array('','js'));
            $community = new \ZL\Community();
            if ($community->getCommunity($communityId)) {
                $this->success($communityData['name'] . '已更新缓存数据!');
            }
        }else
            $this->error('关键词搜索不到小区名字！');
    }
    
    public function reName($community_id=false){
        if($community_id){
            $where1['community_id'] = $community_id;
            $where['id'] = $community_id;
        }
        $cd = M('Community')->where($where)->select();
        $_zones = M('Zone')->where($where1)->select();
        $_bds = M('Building')->where($where1)->select();
        $_units = M('Unit')->where($where1)->select();
        $_rooms = M('Room')->where($where1)->select();
        $_familys = M('Family')->where($where1)->select();
        //转换名字生成数组
        foreach ($cd as $v) {
            $communitys[$v['id']][0] = $v['full_parent_name'];
            $communitys[$v['id']][1] = $v['full_parent_name'] . $v['name'];
        }
        foreach ($_zones as $v) {
            $zones[$v['id']][0] = $communitys[$v['community_id']][1];
            $zones[$v['id']][1] = $communitys[$v['community_id']][1] . $v['name'];
        }
        foreach ($_bds as $v) {
            $bds[$v['id']][0] = $zones[$v['zone_id']][1] ;
            $bds[$v['id']][1] = $zones[$v['zone_id']][1] . $v['name'];
        }
        foreach ($_units as $v) {
            $units[$v['id']][0] = $bds[$v['building_id']][1] ;
            $units[$v['id']][1] = $bds[$v['building_id']][1] . $v['name'];
        }
        foreach ($_rooms as $v) {
            $rooms[$v['id']][0] = $units[$v['unit_id']][1] ;
            $rooms[$v['id']][1] = $units[$v['unit_id']][1] . $v['name'];
        }
        foreach ($_familys as $v) {
            $familys[$v['id']][0] = $rooms[$v['room_id']][1];
            $familys[$v['id']][1] = $rooms[$v['room_id']][1] . $v['name'];
        }
//        myDump($zones);
//        myDump($bds);
//        myDump($units);
//        myDump($rooms);
//        dd($familys);
        //开始转换
        foreach ($zones as $k=>$v) {
            M('Zone')->where(array('id'=>$k))->data(array('full_parent_name'=>$v[0]))->save();
        }
        foreach ($bds as $k=>$v) {
            M('Building')->where(array('id'=>$k))->data(array('full_parent_name'=>$v[0]))->save();
        }
        foreach ($units as $k=>$v) {
            M('Unit')->where(array('id'=>$k))->data(array('full_parent_name'=>$v[0]))->save();
        }
        foreach ($rooms as $k=>$v) {
            M('Room')->where(array('id'=>$k))->data(array('full_parent_name'=>$v[0]))->save();
        }
        foreach ($familys as $k=>$v) {
            M('Family')->where(array('id'=>$k))->data(array('full_parent_name'=>$v[0]))->save();
        }
    }
}